#ifndef PINS_H
#define PINS_H

#include "boards.h"

 #if !MB(5DPRINT)
 #define X_MS1_PIN -1
 #define X_MS2_PIN -1
 #define Y_MS1_PIN -1
 #define Y_MS2_PIN -1
 #define Z_MS1_PIN -1
 #define Z_MS2_PIN -1
 #define E0_MS1_PIN -1
 #define E0_MS2_PIN -1
 #define E1_MS1_PIN -1
 #define E1_MS2_PIN -1
 #define DIGIPOTSS_PIN -1
 #endif


/****************************************************************************************
* Arduino Mega pin assignment
*
****************************************************************************************/
#if IS_RAMPS || MB(3DRAG) || MB(AZTEEG_X3) || MB(AZTEEG_X3_PRO)
#define KNOWN_BOARD 1

//////////////////FIX THIS//////////////
#ifndef __AVR_ATmega1280__
 #ifndef __AVR_ATmega2560__
 #error Oops!  Make sure you have 'Arduino Mega' selected from the 'Tools -> Boards' menu.
 #endif
#endif


// uncomment one of the following lines for RAMPS v1.3 or v1.0, comment both for v1.2 or 1.1
// #define RAMPS_V_1_3
// #define RAMPS_V_1_0


#if (IS_RAMPS && !MB(RAMPS_OLD)) || MB(3DRAG) || MB(AZTEEG_X3) || MB(AZTEEG_X3_PRO)

  #define LARGE_FLASH true

  #if MB(3DRAG)
    #define X_STEP_PIN          12
    #define X_DIR_PIN           11
    #define X_ENABLE_PIN        35
    // #define X_STOP_PIN          24
    #define X_MIN_PIN           24

    #define Y_STEP_PIN          8
    #define Y_DIR_PIN           7
    #define Y_ENABLE_PIN       36
    #define Y_STOP_PIN         25
    #define Y_MIN_PIN          25

    #define Z_STEP_PIN          9
    #define Z_DIR_PIN           6
    #define Z_ENABLE_PIN        42
    #define Z_STOP_PIN          26
    #define Z_MIN_PIN           26

    // #if ENABLED(Z_MIN_PROBE_ENDSTOP)
    //   // Define a pin to use as the signal pin on Arduino for the Z_PROBE endstop.
    //   #define Z_MIN_PROBE_PIN  27
    // #endif


    #define E0_STEP_PIN         5
    #define E0_DIR_PIN          4
    #define E0_ENABLE_PIN      59//A5

    #define X_MS1_PIN         34
    #define X_MS2_PIN         33
    #define X_MS3_PIN         32
    #define X_RESET_PIN       31
    #define X_SLEEP_PIN       18

    #define Y_MS1_PIN         37
    #define Y_MS2_PIN         40
    #define Y_MS3_PIN         41
    #define Y_RESET_PIN       38
    #define Y_SLEEP_PIN       19

    #define Z_MS1_PIN         43
    #define Z_MS2_PIN         47
    #define Z_MS3_PIN         48
    #define Z_RESET_PIN       49
    #define Z_SLEEP_PIN       9

    #define E_MS1_PIN         58
    #define E_MS2_PIN         57
    #define E_MS3_PIN         56
    #define E_RESET_PIN       55
    #define E_SLEEP_PIN       2


    #define SDPOWER            -1
    #define SDSS               63//A9
    #define LED_PIN            -1
    #define FAN_PIN            23
    #define PS_ON_PIN          -1
    #define KILL_PIN           -1

    #define HEATER_0_PIN       10
    #define HEATER_1_PIN       -1
    #define HEATER_2_PIN       -1
    #define TEMP_0_PIN          6   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define TEMP_1_PIN         -1   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define TEMP_2_PIN         -1   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define HEATER_BED_PIN     54//A0
    #define TEMP_BED_PIN        7


    #define LCD_PINS_RS        -1//A15
    #define LCD_PINS_ENABLE    -1//A14
    #define LCD_PINS_D4        67//A13
    #define LCD_PINS_D5        66//A12
    #define LCD_PINS_D6        65//A11
    #define LCD_PINS_D7        -1//A10

    #define BTN_EN1             29
    #define BTN_EN2             28
    #define BTN_ENC             30

    #define SD_DETECT_PIN       62//A8
    #define BEEPER_PIN          39

  #else

    #define X_STEP_PIN          12
    #define X_DIR_PIN           11
    #define X_ENABLE_PIN        35
    #define X_STOP_PIN          60 //A6

    #define Y_STEP_PIN          8
    #define Y_DIR_PIN           7
    #define Y_ENABLE_PIN       36
    #define Y_STOP_PIN         61 //A7

    #define Z_STEP_PIN          9
    #define Z_DIR_PIN           6
    #define Z_ENABLE_PIN        42
    #define Z_STOP_PIN          62 //A8

    #define E0_STEP_PIN         5
    #define E0_DIR_PIN          4
    #define E0_ENABLE_PIN      59//A5

    #define X_MS1_PIN         34
    #define X_MS2_PIN         33
    #define X_MS3_PIN         32
    #define X_RESET_PIN       31
    #define X_SLEEP_PIN       18

    #define Y_MS1_PIN         37
    #define Y_MS2_PIN         40
    #define Y_MS3_PIN         41
    #define Y_RESET_PIN       38
    #define Y_SLEEP_PIN       19

    #define Z_MS1_PIN         43
    #define Z_MS2_PIN         47
    #define Z_MS3_PIN         48
    #define Z_RESET_PIN       49
    #define Z_SLEEP_PIN       9

    #define E_MS1_PIN         58
    #define E_MS2_PIN         57
    #define E_MS3_PIN         56
    #define E_RESET_PIN       55
    #define E_SLEEP_PIN       2


    #define SDPOWER            -1
    #define SDSS               63//A9
    #define LED_PIN            -1
    #define FAN_PIN            23
    #define PS_ON_PIN          -1
    #define KILL_PIN           -1

    #define HEATER_0_PIN       10
    #define HEATER_1_PIN       -1
    #define HEATER_2_PIN       -1
    #define TEMP_0_PIN          6   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define TEMP_1_PIN         -1   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define TEMP_2_PIN         -1   // MUST USE ANALOG INPUT NUMBERING NOT DIGITAL OUTPUT NUMBERING!!!!!!!!!
    #define HEATER_BED_PIN     54//A0
    #define TEMP_BED_PIN        7


    #define LCD_PINS_RS        -1
    #define LCD_PINS_ENABLE    68//A14
    #define LCD_PINS_D4        67//A13
    #define LCD_PINS_D5        66//A12
    #define LCD_PINS_D6        65//A11
    #define LCD_PINS_D7        64//A10

    #define BTN_EN1             29
    #define BTN_EN2             28
    #define BTN_ENC             30

    #define SD_DETECT_PIN       62//A8
    #define BEEPER_PIN          39

#if MB(RAMPS_13_EEB)  //FMM added for Filament Extruder

#endif


    #if MB(AZTEEG_X3_PRO)
      #define E2_STEP_PIN        23
      #define E2_DIR_PIN         25
      #define E2_ENABLE_PIN      40

      #define E3_STEP_PIN        27
      #define E3_DIR_PIN         29
      #define E3_ENABLE_PIN      41

      #define E4_STEP_PIN        43
      #define E4_DIR_PIN         37
      #define E4_ENABLE_PIN      42
    #endif

    #define SDPOWER            -1
    #define SDSS               53
    #define LED_PIN            13
  #endif

  #if MB(RAMPS_13_EFB) || MB(RAMPS_13_EFF) || MB(AZTEEG_X3)
    #define FAN_PIN            9 // (Sprinter config)
  #elif MB(AZTEEG_X3_PRO)
    #define FAN_PIN            11 // Last Heater Pin on board
  #else
    #define FAN_PIN            4 // IO pin. Buffer needed
  #endif

  #if MB(3DRAG) || MB(RAMPS_13_EEF)
    #define FAN_PIN            8
  #endif

  #if MB(RAMPS_13_EFF)
    #define CONTROLLERFAN_PIN  -1 //Pin used for the fan to cool controller
  #endif

  #define PS_ON_PIN          12

  #if defined(REPRAP_DISCOUNT_SMART_CONTROLLER) || defined(G3D_PANEL)
    #define KILL_PIN           41
  #else
    #define KILL_PIN           -1
  #endif

  #if MB(RAMPS_13_EFF)
    #define HEATER_0_PIN       8
  #else
    #define HEATER_0_PIN       10   // EXTRUDER 1
  #endif

  #if MB(RAMPS_13_EFB) || MB(AZTEEG_X3)
    #define HEATER_1_PIN       -1
  #else
    #define HEATER_1_PIN       9    // EXTRUDER 2 (FAN On Sprinter)
  #endif


  #if MB(3DRAG)
    #define HEATER_0_PIN       10
    #define HEATER_1_PIN       12
    #define HEATER_2_PIN       6
  #elif MB(AZTEEG_X3_PRO)
    #define HEATER_2_PIN       16
    #define HEATER_3_PIN       17
    #define HEATER_4_PIN       4
    #define HEATER_5_PIN       5
    #define HEATER_6_PIN       6
    #define HEATER_7_PIN       11
  #else
    #define HEATER_2_PIN       -1
  #endif

  #define TEMP_0_PIN         13   // ANALOG NUMBERING
  #define TEMP_1_PIN         15   // ANALOG NUMBERING
  #if MB(AZTEEG_X3_PRO)
    #define TEMP_2_PIN         12   // ANALOG NUMBERING
    #define TEMP_3_PIN         11   // ANALOG NUMBERING
    #define TEMP_4_PIN         10   // ANALOG NUMBERING
    #define TC1                4    // ANALOG NUMBERING Thermo couple on Azteeg X3Pro
    #define TC2                5    // ANALOG NUMBERING Thermo couple on Azteeg X3Pro
  #else
    #define TEMP_2_PIN         -1   // ANALOG NUMBERING
  #endif

  #if MB(RAMPS_13_EFF) || MB(RAMPS_13_EEF)
    #define HEATER_BED_PIN     -1    // NO BED
  #else
    #if MB(3DRAG)
      #define HEATER_BED_PIN     9    // BED
    #else
      #define HEATER_BED_PIN     8    // BED
    #endif
  #endif

  #define TEMP_BED_PIN       14   // ANALOG NUMBERING

  #ifdef NUM_SERVOS
    #define SERVO0_PIN         11

    #if NUM_SERVOS > 1
      #define SERVO1_PIN         6
    #endif

    #if NUM_SERVOS > 2
      #define SERVO2_PIN         5
    #endif

    #if NUM_SERVOS > 3
      #define SERVO3_PIN         4
    #endif
  #endif

  #if MB(AZTEEG_X3_PRO)
    #define BEEPER 33
  #endif

  #ifdef TEMP_STAT_LEDS
    #if MB(AZTEEG_X3)
      #define STAT_LED_RED       6
      #define STAT_LED_BLUE     11
    #endif
  #endif

 

#else // RAMPS_V_1_1 or RAMPS_V_1_2 as default (BOARD_RAMPS_OLD)

#endif // RAMPS_OLD || RAMPS_13_EFB || RAMPS_13_EEB || RAMPS_13_EFF || 3DRAG

#endif


#ifndef KNOWN_BOARD
#error Unknown MOTHERBOARD value in configuration.h
#endif

//List of pins which to ignore when asked to change by gcode, 0 and 1 are RX and TX, do not mess with those!
#define _E0_PINS E0_STEP_PIN, E0_DIR_PIN, E0_ENABLE_PIN, HEATER_0_PIN,
#if EXTRUDERS > 1
  #define _E1_PINS E1_STEP_PIN, E1_DIR_PIN, E1_ENABLE_PIN, HEATER_1_PIN,
#else
  #define _E1_PINS
#endif
#if EXTRUDERS > 2
  #define _E2_PINS E2_STEP_PIN, E2_DIR_PIN, E2_ENABLE_PIN, HEATER_2_PIN,
#else
  #define _E2_PINS
#endif

#ifdef X_STOP_PIN
  #if X_HOME_DIR < 0
    #define X_MIN_PIN X_STOP_PIN
    #define X_MAX_PIN -1
  #else
    #define X_MIN_PIN -1
    #define X_MAX_PIN X_STOP_PIN
  #endif
#endif

#ifdef Y_STOP_PIN
  #if Y_HOME_DIR < 0
    #define Y_MIN_PIN Y_STOP_PIN
    #define Y_MAX_PIN -1
  #else
    #define Y_MIN_PIN -1
    #define Y_MAX_PIN Y_STOP_PIN
  #endif
#endif

#ifdef Z_STOP_PIN
  #if Z_HOME_DIR < 0
    #define Z_MIN_PIN Z_STOP_PIN
    #define Z_MAX_PIN -1
  #else
    #define Z_MIN_PIN -1
    #define Z_MAX_PIN Z_STOP_PIN
  #endif
#endif

#ifdef DISABLE_MAX_ENDSTOPS
#define X_MAX_PIN          -1
#define Y_MAX_PIN          -1
#define Z_MAX_PIN          -1
#endif

#ifdef DISABLE_MIN_ENDSTOPS
#define X_MIN_PIN          -1
#define Y_MIN_PIN          -1
#define Z_MIN_PIN          -1
#endif

#define SENSITIVE_PINS {0, 1, X_STEP_PIN, X_DIR_PIN, X_ENABLE_PIN, X_MIN_PIN, X_MAX_PIN, Y_STEP_PIN, Y_DIR_PIN, Y_ENABLE_PIN, Y_MIN_PIN, Y_MAX_PIN, Z_STEP_PIN, Z_DIR_PIN, Z_ENABLE_PIN, Z_MIN_PIN, Z_MAX_PIN, PS_ON_PIN, \
                        HEATER_BED_PIN, FAN_PIN,                  \
                        _E0_PINS _E1_PINS _E2_PINS             \
                        analogInputToDigitalPin(TEMP_0_PIN), analogInputToDigitalPin(TEMP_1_PIN), analogInputToDigitalPin(TEMP_2_PIN), analogInputToDigitalPin(TEMP_BED_PIN) }

#endif //__PINS_H
